import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../components/index/Index'
import Login from '../components/login/Login'
import Detail from '../components/detail/Detail'
import Register from '../components/login/Register'
import Rank from '../components/rank/Rank'
import Classify from '../components/classify/Classify'
import MyCenter from '../components/myCenter/MyCenter'
import UploadBookPart from "../components/myCenter/UploadBookPart"
import UploadedBookPart from "../components/myCenter/UploadedBookPart";
import EditUserInfoPart from "../components/myCenter/EditUserInfoPart";
import Search from "../components/search/Search";
import SecurityPart from "../components/myCenter/SecurityPart";
import WantedBookPart from "../components/myCenter/WantedBookPart";

Vue.use(VueRouter);

const routes = [
    {
        path: '/',
        name: 'index',
        component: Index,
        redirect: '/index',
        meta: {
            needLogin: false
        }
    },
    {
        path: '/index',
        name: 'index',
        component: Index,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/login',
        name: 'login',
        component: Login,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/register',
        name: 'register',
        component: Register,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/detail',
        name: 'detail',
        component: Detail,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/rank',
        name: 'rank',
        component: Rank,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/classify',
        name: 'classify',
        component: Classify,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/search',
        name: 'search',
        component: Search,
        meta: {
            needLogin: false
        }
    },
    {
        path: '/myCenter',
        name: 'myCenter',
        component: MyCenter,
        meta: {
            needLogin: true
        },
        children:[
            {
                path: 'uploadBook',
                name: 'uploadBook',
                components: {
                    uploadBook: UploadBookPart
                },
                meta: {
                    needLogin: true
                }
            },
            {
                path: 'uploadedBook',
                name: 'uploadedBook',
                components: {
                    uploadedBook: UploadedBookPart
                },
                meta: {
                    needLogin: true
                }
            },
            {
                path: 'editUploadedBook',
                name: 'editUploadedBook',
                components: {
                    uploadedBook: UploadBookPart
                },
                meta: {
                    needLogin: true
                }
            },
            {
                path: 'editUserInfo',
                name: 'editUserInfo',
                components: {
                    editUserInfo: EditUserInfoPart
                },
                meta: {
                    needLogin: true
                }
            },
            {
                path: 'security',
                name: 'security',
                components:
                    {
                        security: SecurityPart
                    },
                meta: {
                    needLogin: true
                }
            },
            {
                path: 'wantedBook',
                name: 'wantedBook',
                components:
                    {
                        wantedBook: WantedBookPart
                    },
                meta: {
                    needLogin: true
                }
            }
        ]
    }
];

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,
    scrollBehavior (to, from, savedPosition) {
        document.body.style = "overflow: scroll";
        return { x: 0, y: 0 };
    }
});

//全局前置守卫，当发生路由跳转时判断登录状态
router.beforeEach((to, from, next) => {
    //在进行路由跳转时，对跳转路由是否需要登录进行判断
    if (to.matched.some(record => record.meta.needLogin)) {
        //console.log("全局前置守卫：" + sessionStorage.getItem("userInfo"));
        //若sessionStorage中存储了用户信息则表示用户已登录，放行即可。
        if (sessionStorage.getItem("userInfo") !== null && sessionStorage.getItem("userInfo") !== undefined) {
            next();
        }else{
            next({ name: 'login', params: { needLogin: true}});
        }
    }else{
        next();
    }
});

export default router
